/*
 * Inspired by Simple OpenID Plugin
 * http://code.google.com/p/openid-selector/
 */


SAPIEN.action.OpenIdAction = (function () {
	providers = {
		google: {
        	name: 'Google',
			username: false,
        	url: 'https://www.google.com/accounts/o8/id'
	    },
	    yahoo: {
	        name: 'Yahoo',
			username: false,      
	        url: 'http://yahoo.com/'
	    },    
	    aol: {
	        name: 'AOL',     
	        username: true,
	        url: 'http://openid.aol.com/{username}'
	    },
	    openid: {
	        name: 'OpenID',
			username: false,
	        url: null
	    },
	    myopenid: {
	        name: 'MyOpenID',
	        username: true,
	        url: 'http://{username}.myopenid.com/'
	    },
	    livejournal: {
	        name: 'LiveJournal',
	        username: true,
	        url: 'http://{username}.livejournal.com/'
	    },
	    flickr: {
	        name: 'Flickr',        
	        username: true,
	        url: 'http://flickr.com/{username}'
	    },
	    technorati: {
	        name: 'Technorati',
	        username: true,
	        url: 'http://technorati.com/people/technorati/{username}'
	    },
	    wordpress: {
	        name: 'Wordpress',
	        username: true,
	        url: 'http://{username}.wordpress.com/'
	    },
	    blogger: {
	        name: 'Blogger',
	        username: true,
	        url: 'http://{username}.blogspot.com/'
	    },
	    verisign: {
	        name: 'Verisign',
	        username: true,
	        url: 'http://{username}.pip.verisignlabs.com/'
	    },
	    vidoop: {
	        name: 'Vidoop',
	        username: true,
	        url: 'http://{username}.myvidoop.com/'
	    },
	    verisign: {
	        name: 'Verisign',
	        username: true,
	        url: 'http://{username}.pip.verisignlabs.com/'
	    },
	    claimid: {
	        name: 'ClaimID',
	        username: true,
	        url: 'http://claimid.com/{username}'
	    }
	};
	msgUsername = null;
	msgUrl = null;
	provider = null;
	return {
		submit: function() {
			if (provider.url === null) {
				$('#openid').val($('#openid_input').val());
			} else {
				$('#openid').val(provider.url.replace('{username}', $('#openid_input').val()));
			}
			$('#openid_form').submit();
		},
		selectProvider: function (name) {
			provider =  providers[name];
			$('#openid_btns a').removeClass('highlight');
			$('#' + name).addClass('highlight');
			if (provider.url === null) {
				$('#openid_input').val('http://');
				$('#openid_username_message').hide();
				$('#openid_url_message').show();
				$('#openid_input_area').show();
				$('#openid_input').focus();
			} else if (provider.username) {
				$('#openid_input').val('');
				$('#openid_url_message').hide();
				$('#openid_username_message').html(msgUsername.replace('{provider}',provider.name)).show();
				$('#openid_input_area').show();
				$('#openid_input').focus();
			} else {
				$('#openid').val(provider.url);
				$('#openid_form').submit();
			}
		},
		init: function() {
			msgUsername = $('#openid_username_message').html();
			msgUrl = $('#openid_url_message').html();
			$('#openid_btns a').each(function(index, val) {
				var el = $(this);
				var id = el.attr('class');
				el.click(function(){
					SAPIEN.action.OpenIdAction.selectProvider(id)
				});
			});
			$('#openid_submit').click(function() {SAPIEN.action.OpenIdAction.submit()});
		}
	};
})();
$(document).ready(function () {
	SAPIEN.action.OpenIdAction.init();
});